// pages/shop/shop.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: '',
    serchType: {
      text: 'map',
      content: '',
      placehould: '查找地点、公交、地铁'
    },

    price_index:0,
    xiji_index:0,
    menuContent: [{ //自造下拉菜单数据
      title: "附近",
      content: ['默认', '500米', '1000米', '2000米', '5000米']
    }, {
      title: "价格",
      content: [],
      xiji: [{
        name: '不限',
        id: 1
      }, {
        name: '经济型',
        id: 2
      }, {
        name: '二星/实惠',
        id: 3
      }, {
        name: '三星/舒适',
        id: 4
      }, {
        name: '四星/高档',
        id: 5
      }, {
        name: '五星/豪华',
        id: 6
      }],
      price: [{
        name: '不限',
        id: 1
      }, {
        name: '0-150',
        id: 2
      }, {
        name: '150-300',
        id: 3
      }, {
        name: '300-500',
        id: 4
      }, {
        name: '500-800',
        id: 5
      }, {
        name: '800+',
        id: 6
      }],

    }, {
      title: "筛选",
      content: ['全部美食', '闽南风味菜', '火锅', '西餐', '小吃名点']
    }, {
      title: "排序",
      content: ['智能排序', '距离优先', '人气优先', '好评优先']
    }],
    mainIndex: -1,
    menutabIndex: null, //主菜单样式(箭头转向)
    subMenuDisplay: null, //下拉菜单数据
    subMenuTakeup: [false, false, false,false], //下拉菜单样式
    subMenuHighLight: [{ //菜单高亮样式
      supIndex: 0,
      subIndex: 0
    }, {
      supIndex: 1,
      subIndex: 0
    }, {
      supIndex: 2,
      subIndex: 0
    },{
      supIndex: 3,
      subIndex: 0
    }],

    curNav:0,
    shaixuan_menu_list:[
      {
        name:'酒店类型',
        id:100,
        childList:[{
          desc: '经济连锁',
          id:1
        },{
          desc: '连锁品牌',
          id: 2
        },{
          desc: '酒店式公寓',
          id: 3 
        },{
          desc: '奢华酒店',
          id: 4 
        },{
          desc: '家庭旅馆',
          id: 5 
        }]
      },{
        name: '房型',
        id: 200,
        childList: [{
          desc: '一室',
          id: 1
        }, {
          desc: '二室',
          id: 2
        }, {
          desc: '三室',
          id: 3
        }, {
          desc: '四室',
          id: 4
        }, {
          desc: '四室以上',
          id: 5
        }] 
      },{
        name: '早餐',
        id: 300,
        childList: [{
          desc: '含早餐',
          id: 1
        }, {
          desc: '不含早餐',
          id: 2
        }]
      }
    ],
    selected_items:[],
   
    pageIndex: 1,

    isHiddenMes1: true,

    hideHeader: true,
    hideBottom: true,
    refreshTime: '', // 刷新的时间
    currentPage: 1,//当前页数  默认是1
    allPages: '',    // 总页数
    loadMoreData: '加载更多……',
    rows: 20, //每页条数
    list: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var date = new Date();
    this.setData({
      refreshTime: date.toLocaleTimeString()
    })

    this.getListData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

    var aa = this.data.shaixuan_menu_list[this.data.curNav].childList;

    this.setData({
      selected_items:aa
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  searchValue: function (e) {
    console.log(e);

    var text = e.detail;
  },
  onGetValue: function () {
    wx.navigateTo({
      url: '../nearsearch/nearsearch',
    })
  },
 
  // 点击显示下拉菜单
  tapMainMenu: function(e) {
    var index = parseInt(e.currentTarget.dataset.index);

    if (this.data.mainIndex != index && this.data.mainIndex>-1 ){
      var str0 = "subMenuTakeup[" + this.data.mainIndex + "]";
      this.setData({
        [str0]: false
      })
    }

    this.setData({
      mainIndex: index
    })

    var str = "subMenuTakeup[" + index + "]";
    if (this.data.subMenuTakeup[index] == true) {
      this.setData({
        [str]: false,
        menutabIndex: null,
        subMenuDisplay: null
      })

      return
    }


    this.setData({
      menutabIndex: index,
      subMenuDisplay: index,
      [str]: true
    })

    console.log(index, this.data.subMenuTakeup);

  },
  //选中子菜单
  tapSubMenu: function(e) {

    var indexAry = e.currentTarget.dataset.index.split('-');
    console.log(indexAry);
    //父级索引值
    var index1 = parseInt(indexAry[0]);
    //子级索引值
    var index2 = parseInt(indexAry[1]);
    var str = "subMenuHighLight[" + index1 + "].subIndex";
    var str1 = "subMenuTakeup[" + index1 + "]";
    this.setData({
      [str]: index2,
    })
    console.log(this.data.subMenuHighLight[index1].subIndex, '-------');
    var that = this;
    setTimeout(function() {
      that.setData({
        menutabIndex: null,
        subMenuDisplay: null,
        [str1]: false
      })
    }, 500)

  },
  /*
  * 酒店-商户-详情
  */
  orderDetail: function(e) {
    console.log(e, "商户详情");
    wx.navigateTo({
      url: '../hoteldetail/hoteldetail?id=' + e.currentTarget.dataset.id
    })
  },
  reset:function(){
    console.log( "重置");
    this.setData({
      price_index: 0,
      xiji_index: 0,
    })
  },
  confirmIt:function(){
    console.log('确定', this.data.menutabIndex);

    var str = "subMenuTakeup[" + this.data.menutabIndex + "]";
    this.setData({
      [str]: false,
      menutabIndex: null,
      subMenuDisplay: null
    })
    console.log(this.data.subMenuTakeup); 
  },
  priceItem:function(e){
    console.log('价格筛选', e.currentTarget.dataset.index);

    this.setData({
      price_index: e.currentTarget.dataset.index
    })

  },
  xijiItem:function(e){
    console.log('星级筛选', e.currentTarget.dataset.index);

    this.setData({
      xiji_index: e.currentTarget.dataset.index
    })
  },
  //筛选--切换左侧内容
  switchRightTab:function(e){
    console.log(e.currentTarget.dataset.index);
    
    var index = e.currentTarget.dataset.index;
    
    if (this.data.curNav==index)return;


    var aa = this.data.shaixuan_menu_list[index].childList;

    this.setData({
      curNav: index,
      selected_items: aa
    })
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  // 上拉加载更多
  loadMore: function () {
    console.log('上拉加载更多');

    var self = this;
    // 当前页是最后一页
    if (self.data.currentPage == self.data.allPages) {
      self.setData({
        loadMoreData: '已经到顶'
      })
      return;
    }
    setTimeout(function () {
      console.log('上拉加载更多');
      var tempCurrentPage = self.data.currentPage;
      tempCurrentPage = tempCurrentPage + 1;
      self.setData({
        currentPage: tempCurrentPage,
        hideBottom: false
      })
      self.getListData();
    }, 300);
  },
  // 下拉刷新
  refresh: function (e) {
    var self = this;
    setTimeout(function () {
      console.log('下拉刷新');
      var date = new Date();
      self.setData({
        currentPage: 1,
        refreshTime: date.toLocaleTimeString(),
        hideHeader: false
      })
      self.getListData();
    }, 300);
  },
  getListData:function(){
    var self = this;
    var pageIndex = self.data.currentPage;

    app.apiPost(app.apiList.hotel_list, {
      content:{
        pageNum: pageIndex,
        pageSize: self.data.rows
      }
    }, (res) => {
      console.log(res, 'meishiList');

      var list = res.content.list;

      if (pageIndex == 1) {// 下拉刷新
        self.setData({
          allPages: res.content.total,
          list: list,
          hideHeader: true
        })

      } else {// 加载更多
        var tempArray = self.data.list;
        tempArray = tempArray.concat(list);
        self.setData({
          allPages: res.content.total,
          list: tempArray,
          hideBottom: true
        })
      }

      if (self.data.list.length > 0) {
        self.setData({
          isHiddenMes1: false
        })
      }

    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading();
    var that = this;
    that.setData({
      pageIndex: 1, // 每次触发下拉事件page=0
    })
    //发起请求
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    app.loading();
    that.setData({
      // 页数+1
      pageIndex: that.data.pageIndex + 1
    })
    // 上拉获取更多数据
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})